package com.ktjy.ruoyidata.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktjy.ruoyidata.mapper.MyClientMapper;
import com.ktjy.ruoyidata.domain.MyClient;
import com.ktjy.ruoyidata.service.IMyClientService;

/**
 * 客户Service业务层处理
 * 
 * @author t382
 * @date 2025-04-29
 */
@Service
public class MyClientServiceImpl implements IMyClientService 
{
    @Autowired
    private MyClientMapper myClientMapper;

    /**
     * 查询客户
     * 
     * @param clientId 客户主键
     * @return 客户
     */
    @Override
    public MyClient selectMyClientByClientId(Long clientId)
    {
        return myClientMapper.selectMyClientByClientId(clientId);
    }

    /**
     * 查询客户列表
     * 
     * @param myClient 客户
     * @return 客户
     */
    @Override
    public List<MyClient> selectMyClientList(MyClient myClient)
    {
        return myClientMapper.selectMyClientList(myClient);
    }

    /**
     * 新增客户
     * 
     * @param myClient 客户
     * @return 结果
     */
    @Override
    public int insertMyClient(MyClient myClient)
    {
        // 生成客户编码
        String clientType = myClient.getClientType();
        String prefix = "C"; // 默认为企业类型
        if ("INDIVIDUAL".equals(clientType)) { // 根据实际字典值调整
            prefix = "P";
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        String datePart = sdf.format(new Date());

        // 生成4位随机数
        String randomPart = String.format("%04d", new Random().nextInt(10000));

        String clientCode = prefix + datePart + randomPart;
        myClient.setClientCode(clientCode);

        myClient.setCreateTime(DateUtils.getNowDate());
        return myClientMapper.insertMyClient(myClient);
    }

    /**
     * 修改客户
     * 
     * @param myClient 客户
     * @return 结果
     */
    @Override
    public int updateMyClient(MyClient myClient)
    {
        myClient.setUpdateTime(DateUtils.getNowDate());
        return myClientMapper.updateMyClient(myClient);
    }

    /**
     * 批量删除客户
     * 
     * @param clientIds 需要删除的客户主键
     * @return 结果
     */
    @Override
    public int deleteMyClientByClientIds(Long[] clientIds)
    {
        return myClientMapper.deleteMyClientByClientIds(clientIds);
    }

    /**
     * 删除客户信息
     * 
     * @param clientId 客户主键
     * @return 结果
     */
    @Override
    public int deleteMyClientByClientId(Long clientId)
    {
        return myClientMapper.deleteMyClientByClientId(clientId);
    }
}
